package cn.apestar.modules.common.systemannouncement.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import cn.apestar.common.config.Global;
import cn.apestar.core.http.AppType;
import cn.apestar.modules.common.systemannouncement.entity.PushMessage;

@Service
@Transactional(readOnly = true)
public class SystemAnnouncementApiService {

    // 系统公告
    private static String sendPathColony = "/common/common/sendPushInformation";

    private static String sendPath = "/common/common/sendPushInformation";

    private static SerializeConfig mapping = new SerializeConfig();

    private static String dateFormat;
    static {
        dateFormat = "yyyy-MM-dd HH:mm:ss";
        mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
    }

    public String getPath() {
        if (AppType.ANT_CUSTOMER.getValue().equals(Global.getAppType())) {
            return sendPathColony;
        } else
            return sendPath;
    }

    @Autowired
    RestTemplate restTemplate;

    /**
     * 发送推送
     * @author lvh 2016年6月23日 上午9:18:01</br>
     * @return void
     */
    public void sendPush(String title, String content) {
        String url = Global.getConfig("USER_CLEAR_URL") + getPath();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        PushMessage s = new PushMessage();
        s.setContent(content);
        s.setTitle(title);
        s.setAppType(Global.getConfig("APP_TYPE"));
        HttpEntity<PushMessage> formEntity = new HttpEntity<PushMessage>(s, headers);
        String listString = restTemplate.postForObject(url, formEntity, String.class).toString();
        System.out.print(listString);
    }

}
